package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.OwnSelectProcessingUnitAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerCompanyBean;
import com.renwei.yunlong.bean.ServiceCompanyBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择处理企业--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class SelectProcessingUnitActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;

    private ACache mCache;

    private OwnSelectProcessingUnitAdapter adapter;

    private Intent intent;
    LoginBean bean;
    String companyType;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_owner);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        mCache = ACache.get(this);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择处理企业");
        rlSearch.setOnClickListener(this);

        bean = (LoginBean) mCache.getAsObject("loginBean");
        companyType = mCache.getAsString("companyType");
        if ("1".equals(companyType)) {//业主
            if (bean != null) {
                //{"exeType":1,"useFlag":1,"type":2,"ownerCode":20170905-143432-71,"currentUserId": 86b03127-a875-4898-bdac-1d1ca8031b9e}
                String jsonBean = "{exeType:\"" + "1" + "\",useFlag:\"" + "1" + "\",type:\"" + "2" + "\",ownerCode:\"" + bean.getRows().getOwnerCode() + "\",currentUserId:\"" + bean.getRows().getEmployeeId() + "\",serverName:\"" + "" + "\"}";
                ownerSelectProcesUnitHttp(jsonBean);
            }
        }
    }

    //业主工单处理单位
    private void ownerSelectProcesUnitHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectProcessingUnitActivity.this);
        hp.setUrl(Api.OWNER_QUERYSERVERINFO_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    private void setOwnerListDate(List<ServiceCompanyBean.Rows> listDate) {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new OwnSelectProcessingUnitAdapter(SelectProcessingUnitActivity.this, listDate);
        adapter.setOnItemClickListener(new OwnSelectProcessingUnitAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String companyName,String bindFlag,String companyCode) {
                intent = new Intent();
                intent.putExtra("companyName", companyName);
                mCache.put("ownerBindFlag",bindFlag);
                mCache.put("ownerCompanyCode",companyCode);
                setResult(RESULT_OK, intent);
                SelectProcessingUnitActivity.this.finish();
            }
        });
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_search:
                String search = etSearch.getText().toString().trim();
                if ("1".equals(companyType)) {//业主
                    if (bean != null) {
                        if (!TextUtils.isEmpty(search)){
                            //{"exeType":1,"useFlag":1,"type":2,"ownerCode":20170905-143432-71,"currentUserId": 86b03127-a875-4898-bdac-1d1ca8031b9e}
                            String jsonBean = "{exeType:\"" + "1" + "\",useFlag:\"" + "1" + "\",type:\"" + "2" + "\",ownerCode:\"" + bean.getRows().getOwnerCode() + "\",currentUserId:\"" + bean.getRows().getEmployeeId() + "\",serverName:\"" + search + "\"}";
                            ownerSelectProcesUnitHttp(jsonBean);
                        }
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                ServiceCompanyBean bean = new Gson().fromJson(result, ServiceCompanyBean.class);
                if (bean.getMessage().getCode() == 200) {
                    setOwnerListDate(bean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
